// by Xeno
if (!isServer) exitWith {};

_pos = [0,0,0];

d_island_center = getArray (configFile >> "CfgWorlds" >> worldName >> "centerPosition");

d_check_boxes = [];
execVM "server\boxhandling_old.sqf";

_debug = true;
_debug = ((d_global_debug > 9) && _debug);
[] spawn {
	// sleep (10);
	sleep (150 + random (150));
	[true,_debug] execVM "server\randomconvoy.sqf";
	sleep (5 + random(5));
	[false,_debug] execVM "server\randomconvoy.sqf";
	// sleep (10 + random(10));
	// [] execVM "server\randomairspawn.sqf";
	sleep (5 + random(5));
	[_debug] execVM "server\randomroadblocks.sqf";
	sleep (5 + random(5));
	[_debug] execVM "server\randomcamps.sqf";
};

execVM "server\spawnvehicles.sqf";

// Registers every minute or so the last land position of a player
[] spawn {
	private ["_ap","_pos","_currpos","_players","_indexer","_positions","_names","_index"];
	d_player_store setVariable ["player_positions",[[],[],[]]];
	while {true} do {
		_players = d_player_store getVariable "player_positions";
		if (count _players > 0) then {
			_indexer = _players select 0;
		} else {
			_indexer = [];
		};
		if (count _players > 1) then {
			_positions = _players select 1;
		} else {
			_positions = [];
		};
		if (count _players > 2) then {
			_names = _players select 2;
		} else {
			_names = [];
		};
		{
			_ap = missionNamespace getVariable _x;
			_index = _indexer find _x;
			_pos = getMarkerPos "base_spawn_1";
			if (_index > 0) then {
				_pos = _positions select _index;
			};
			_currpos = getPosATL _ap;
			if (isNil "_currpos") then {_currpos = _pos};
			if (surfaceIsWater[(_currpos select 0),(_currpos select 1)]) then {
				_currpos = _pos;
			};
			if (_index >= 0) then {
				_positions set [_index, _currpos];
				if (alive _ap) then { _names set [_index, name _ap]; };
			} else {
				_indexer set [count _indexer,_x];
				_positions set [count _positions, _currpos];
				if (alive _ap) then { _names set [count _names, name _ap]; };
			};
			sleep 0.5;
		}foreach d_player_entities;
		d_player_store setVariable ["player_positions",[_indexer,_positions,_names]];
		// diag_log Format["Player positions: %1",(d_player_store getVariable "player_positions")];
		sleep 10;
	};
};

// TODO: modify the following statement to simply output a message to the hint
//[d_base_array select 0, [d_base_array select 1, d_base_array select 2, d_base_array select 3, true], [d_enemy_side, "PRESENT", true], ["'Man' countType thislist > 0 || 'Tank' countType thislist > 0 || 'Car' countType thislist > 0", "d_kb_logic1 kbTell [d_kb_logic2,d_kb_topic_side,'BaseUnderAtack',true]", ""]] call XfCreateTrigger;

_center = getArray (configFile>>"CfgWorlds">>worldName >> "centerPosition");
_trigger = [_center, [_center select 1, _center select 2, 0, true], ["ANY", "PRESENT", true], ["", "", ""]] call XfCreateTrigger;
[_trigger] execVM "scripts\BodiesCleanUp.sqf";

If (count d_pos_ai_hut > 0) then {
	_D_AI_HUT = false;
	_D_AI_HUT = objNull;
	while { (_D_AI_HUT distance [0,0,0] <= 0) || (isnil "x_creategroup")} do {
		_D_AI_HUT = ["D_AI_HUT",objNull] call XNetGetJIP;
		sleep 0.54678;
	};
	_D_AI_HUT = ["D_AI_HUT",objNull] call XNetGetJIP;
	diag_log "Creating AI Officers";
	_grp_ai = [d_own_side] call x_creategroup;
	_ai_type = "USMC_Soldier_Officer";
	
	_ai_pos = _D_AI_HUT modelToWorld [0,2,0];
	_ai = [_ai_type, _grp_ai, _ai_pos] call XfCreateUnit;
	_ai disableAI "MOVE";
	removeAllWeapons _ai;
	_ai allowDamage false;
	_ai setCaptive true;
	["D_AI_HALO_OFFICER",_ai] call XNetSetJIP;
	diag_log format["AI Officer: %1",[_ai, _ai_pos]];

	_ai_pos = _D_AI_HUT modelToWorld [0,-4,0];
	_ai = [_ai_type, _grp_ai, _ai_pos] call XfCreateUnit;
	_ai disableAI "MOVE";
	removeAllWeapons _ai;
	_ai allowDamage false;
	_ai setCaptive true;
	["D_AI_GROUP_OFFICER",_ai] call XNetSetJIP;
	diag_log format["AI Officer: %1",[_ai, _ai_pos]];
};
